package com.software.shipper.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

import com.software.common.CommonUtils;
import com.software.form.inquiry.sagent.dao.InquirySagentTransaction;
import com.software.form.inquiryform.dao.InquiryFormTransaction;

public class InquiryServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		InquiryFormTransaction ift = (InquiryFormTransaction) CommonUtils.getBean("inquiryformTransaction");
		JSONObject json = CommonUtils.readJSONStringFromRequestBody(request, response);
		try{
			String no = json.getString("no");
			String email = json.getString("shipper_email");
			String shipper = json.getString("shipper");
			String departure = json.getString("departure");
			String dest = json.getString("dest");
			String cargo = json.getString("cargo");
			String weight = json.getString("weight");
			String m3 = json.getString("m3");
			String info = json.getString("info");
			String agents = json.getString("agents");
			if(ift.add(no, shipper, email, departure, dest, cargo, weight, m3, info)){
				String[] shippingAgents = agents.split(" ");
				InquirySagentTransaction ist = (InquirySagentTransaction)CommonUtils.getBean("inquirySagentTransaction");
				for(int i=0; i<shippingAgents.length; i++){
					ist.add(no, shippingAgents[i], 1);
				}
				CommonUtils.outputJsonData(1, response);
			}else
				CommonUtils.outputJsonData(0, response);
		}catch(JSONException je){
			je.printStackTrace();
			try {
				CommonUtils.outputJsonData(0, response);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
